package screen_calibration;
import java.awt.*;


/**
 * Was made to provide a graphic in side the UI.
 * This particular class inherites from canvas and therefore can paint 
 * the gray levels while listening to the user action.
 * @author Alex Frid alex.frid@gmail.com; Ran Dror
 */
final public class GammaCanvas extends Canvas{
    
    private int r_lvl;
    private int g_lvl;
    private int b_lvl;
    
    public int get_r_Lvl(){
        return r_lvl;
    }
    
    public int get_g_Lvl(){
        return g_lvl;
    }
    
    public int get_b_Lvl(){
        return b_lvl;
    }
    
    public void set_r_Lvl(int l){
        r_lvl = l;
    }
    
    public void set_g_Lvl(int l){
        g_lvl = l;
    }
    
    public void set_b_Lvl(int l){
        b_lvl = l;
    }
    
    public void setGrayLvl(int l ){
        r_lvl = l;
        g_lvl = l;
        b_lvl = l;
    }
    
    public int getGrayLvl(){
        return (int)((r_lvl+b_lvl+g_lvl)/3);
    }
    
    public void update (Graphics gc)
    {
        paint(gc);
    }
    
    /** 
     * 	Fills the middle of the given Graphics obgect with 
     *  a given colour 
     **/
    public void fillMiddle(Graphics g){
        int x_start = (int)size().getWidth()/4;  // start of the x point
        int y_start = (int)size().getHeight()/4; // start of the y point
        
        // maximum size of the object
        int max_x = size().width, max_y = size().height;
        
        g.setColor(Color.white);
        
        // fill with dithered lines except the inner rectangle
        for (int i=0; i <= max_y; i+=2) {
            if(i>=y_start & i<(y_start+y_start*2)){
                g.drawLine(0,i,x_start,i);
                g.drawLine(3*x_start,i,max_x,i);
            }
            else
                g.drawLine(0,i,max_x,i);
        }
        
        // fill the inner rectangle 
        g.setColor(new Color(r_lvl, g_lvl, b_lvl));
        g.fillRect(x_start, y_start, (int)size().getWidth()/2+1, (int)size().getHeight()/2+1);
    }
    
    /** Fill with chess board dithering */
    public void fillChessBoard(Graphics g){
        int x_start = (int)size().getWidth()/4;  // start of the x point
        int y_start = (int)size().getHeight()/4; // start of the y point
        
        // maximum size of the object
        int max_x = size().width, max_y = size().height;
        
        g.setColor(Color.white);
        // fill with dithered lines except the inner rectangle
        for (int i=0; i <= max_y; i++) {
            for(int j=0; j<=max_x; j++){
                // draw the chess board every where except the inner rectangle
                if((i<y_start || i>=(3*y_start) || (j<x_start || j>=3*x_start))){
                    if(i%2 == 1 & j%2 == 1)  
                        g.drawLine(j,i,j,i);
                    else
                        if( i%2 == 0 & j%2 == 0)
                            g.drawLine(j,i,j,i);
                }                
            }
        }
        // fill the inner rectangle 
        g.setColor(new Color(r_lvl, g_lvl, b_lvl));
        g.fillRect(x_start, y_start, (int)size().getWidth()/2, (int)size().getHeight()/2);
        
    }
    /** The paint process:
     *  paints the dithered lines and the inner rectangle 
     **/
    public void paint (Graphics gc)
    {
        int max_x = size().width, max_y = size().height;
        
        if ((r_lvl >= 0)&(g_lvl >= 0)&(b_lvl >= 0)) {
            // Fills with the chessboard dithering 
            //fillChessBoard(gc);
            
            // Fills with horisontal lines dithering
            fillMiddle(gc);
        }
        else { 
            // prints the horisontal dithering lines
            gc.setColor(Color.white);
            for (int i=0; i <= max_y; i+=2) {
                gc.drawLine(0,i,max_x,i);
            }
        }
    }
}
